package tgc.edu.tms.service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Base64Utils;
import org.springframework.web.multipart.MultipartFile;

import tgc.edu.tms.custom.CommonService;
import tgc.edu.tms.entity.TtbPicture;
import tgc.edu.tms.repository.TtbPictureRepository;

@Service
public class TtbPictureService extends CommonService<TtbPicture,Integer>{
	@Autowired
	private TtbPictureRepository ttb_PictureDAO;
	private String path="c:/java";

	public List<TtbPicture> findByRecordId(Integer id) {
		return ttb_PictureDAO.findByRecordId(id);
	}
	
	@Transactional
	public void save(MultipartFile file) {
		TtbPicture model = new TtbPicture();
		String filename = file.getOriginalFilename();
		String uuid = UUID.randomUUID().toString();
		model.setFilename(filename);
		model.setUuid(uuid);
		ttb_PictureDAO.save(model);
		File path = new File(this.path);
		if(!path.isDirectory()) {
			path.mkdir();
		}
		File saveFile = new File(path, uuid);
		try {
			file.transferTo(saveFile);
		} catch (IllegalStateException | IOException e) {
			e.printStackTrace();
		}
		
	}
//下载
	public void download(Integer id, HttpServletRequest request, HttpServletResponse response) {
		TtbPicture model = findById(id);
		String filename = model.getFilename();
		filename=getStr(request,filename);
		String uuid = model.getUuid();
		File file = new File(path, uuid);
		if(file.exists()) {
			FileInputStream fis;
			try {
				fis = new FileInputStream(file);
				response.setContentType("application/x-msdownload");
				response.addHeader("Content-Disposition", "attachment; filename=" + filename);
				ServletOutputStream out = response.getOutputStream();
				byte[] buf=new byte[2048];
				int n=0;
				while((n=fis.read(buf))!=-1){
					out.write(buf, 0, n);
				}
				fis.close();
				out.flush();
				out.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}catch (IOException e) {
				e.printStackTrace();
			}
		
		}

	}

private String getStr(HttpServletRequest request, String filename) {
	String downloadFileName = null;
	String agent = request.getHeader("USER-AGENT");
		 try {
			 if(agent != null && agent.toLowerCase().indexOf("firefox")>0) {
			downloadFileName = "=?UTF-8?B?" + Base64Utils.encodeToString(filename.getBytes("UTF-8")) + "?=";
			 }else {
				 downloadFileName =  java.net.URLEncoder.encode(filename, "UTF-8");
			 }
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	return downloadFileName;
}
}
